package com.xinwei.utils;

import org.apache.poi.hssf.usermodel.*;

public class ExcelUtil {

    public static HSSFWorkbook getHSSFWOrkbook(String sheetName, String []title, String [][]values){
        //创建一个HSSFWorkbook,对应一个excel文件
        HSSFWorkbook wb = new HSSFWorkbook();

        //在HSSFWorkbook中添加一个sheet,对应excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        HSSFRow row = sheet.createRow(0);

        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        HSSFCell cell = null;
        for(int k = 0; k < title.length; k++){
            cell = row.createCell(k);
            cell.setCellValue(title[k]);
            cell.setCellStyle(style);
        }

        for(int i = 0; i < values.length; i++){
            row = sheet.createRow(i + 1);
            for(int j = 0; j < values[i].length; j++){
                row.createCell(j).setCellValue(values[i][j]);
            }
        }

        return wb;
    }

}
